/*
 * @(#)XfslSetting.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsl.model;

import xfuzzy.lang.*;

/**
 * Selecci�n de par�metros a ajustar
 * 
 * @author Francisco Jos� moreno Velo
 *
 */
public class XfslSetting {

	//----------------------------------------------------------------------------//
	//                            COSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo de deselecci�n de par�metros
	 */
	public static final int DISABLE = 0;
	
	/**
	 * C�digo de elecci�n de par�metros
	 */
	public static final int ENABLE = 1;
	
	/**
	 * C�digo de error
	 */
	public static final int NOT_SELECTED = -1;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo de la elecci�n 
	 */
	private int code;
	
	/**
	 * Tipo de variables a las que se refiere la elecci�n
	 */
	private String type;
	
	/**
	 * Etiquetas ling��sticas a las que se refiere la elecci�n
	 */
	private String mf;
	
	/**
	 * Par�metro de las etiquetas a las que se refiere la elecci�n
	 */
	private int param;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor a partir de objetos
	 */
	public XfslSetting(Type type,ParametricFunction mf,int param,boolean enable) {
		if(type != null) this.type = type.getName(); else this.type = null;
		if(mf != null) this.mf = mf.toString(); else this.mf = null;
		this.param = param;
		if(enable) this.code = ENABLE; else this.code = DISABLE;
	}

	/**
	 * Constructor a partir de c�digo
	 */
	public XfslSetting(String def, boolean enable) {
		String tname = def.substring(0, def.indexOf("."));
		String mfname = def.substring(def.indexOf(".")+1, def.lastIndexOf("."));
		String pname = def.substring(def.lastIndexOf(".")+1);

		if(tname.equals("ANY")) this.type = null; else this.type = tname;
		if(mfname.equals("ANY")) this.mf = null; else this.mf = mfname;
		if(pname.equals("ANY")) this.param = NOT_SELECTED;
		else this.param = Integer.parseInt(pname);
		if(enable) this.code = ENABLE; else this.code = DISABLE;
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Descripci�n que aparece en la ventana
	 */
	public String toString() {
		String tname = (type == null? "ANY": type);
		String mfname = (mf == null? "ANY": mf);
		String pname = (param == NOT_SELECTED? "ANY": ""+param);

		if(code == ENABLE) return "enable: "+tname+"."+mfname+"."+pname;
		else return "disable: "+tname+"."+mfname+"."+pname;
	}

	/**
	 * Descripci�n que aparece en el fichero de configuraci�n
	 */
	public String toXML() {
		String eol = System.getProperty("line.separator", "\n");
		String xml = "";

		String tname = (type == null? "ANY": type);
		String mfname = (mf == null? "ANY": mf);
		String pname = (param == NOT_SELECTED? "ANY": ""+param);
		String value = tname+"."+mfname+"."+pname;

		if(code == ENABLE) {
			xml += "\t\t<enable value=\""+value+"\">"+eol;
			xml += "\t\t</enable>"+eol;
		} else {
			xml += "\t\t<disable value=\""+value+"\">"+eol;
			xml += "\t\t</disable>"+eol;			
		}

		return xml;
	}

	/**
	 * Aplica la selecci�n a los tipos de una especificaci�n
	 */
	public void set(Type[] tp) {
		for(int i=0; i<tp.length; i++) {
			if(type != null && !type.equals(tp[i].getName())) continue;
			Family[] fam = tp[i].getFamilies();
			for(int j=0; j<fam.length; j++) {
				if(mf != null && !mf.equals(fam[j].toString())) continue;
				Parameter[] pm = fam[j].getParameters();
				if(param == NOT_SELECTED)
					for(int k=0;k<pm.length;k++) pm[k].setAdjustable( (code == ENABLE) );
				else if(param<=pm.length) pm[param-1].setAdjustable( (code == ENABLE));
			}
			ParamMemFunc[] pmf = tp[i].getParamMembershipFunctions();
			for(int j=0; j<pmf.length; j++) {
				if(mf != null && !mf.equals(pmf[j].toString())) continue;
				Parameter[] pm = pmf[j].getParameters();
				if(param == NOT_SELECTED)
					for(int k=0;k<pm.length;k++) pm[k].setAdjustable( (code == ENABLE) );
				else if(param<=pm.length) pm[param-1].setAdjustable( (code == ENABLE));
			}
		}
	}
}

